﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Common.Utilities.Dispatcher;

namespace Workflows.Components.TextMining.EnumerationObjects
{
    /// <summary>
    /// 
    /// </summary>
    public partial class PdfScanEditor : Form, IComponentEditor
    {
        private object _ActivityNode;
        private PdfScanComponent _PdfScanComponent;

        /// <summary>
        /// 
        /// </summary>
        public PdfScanEditor()
        {
            InitializeComponent();
        }

        #region IComponentEditor Members

        public event UpdateComponentSettings UpdateComponentSettingEvent;

        /// <summary></summary>
        public object OwnerActivityNode
        {
            get { return _ActivityNode; }
            set { _ActivityNode=value; }
        }

        /// <summary></summary>
        public Type WorkflowComponentType
        {
            get { return typeof (PdfScanComponent); }
        }

        /// <summary></summary>
        public void LoadSettings(GlobalVariable[] globalVars, WorkflowComponentBase[] fromComponents,
                                 object activityNode, WorkflowComponentBase wfComponent)
        {
            this._ActivityNode = activityNode;
            this._PdfScanComponent = (PdfScanComponent) wfComponent;

            this.txt_FileListXmlFilePath.Text = this._PdfScanComponent.FileListInputXmlFilePath;
            this.txt_OutputFilePath.Text = this._PdfScanComponent.ResultFilePath;
            this.txt_Pattern.Text = this._PdfScanComponent.Pattern;
            this.txt_LogFilePath.Text = this._PdfScanComponent.LogFilePath;
            this.txt_ErrorFilePath.Text = this._PdfScanComponent.ErrorFilePath;
            this.num_MinScore.Value =(decimal) this._PdfScanComponent.MatchScoreThreshold;
            this.num_ThreadPoolSize.Value = this._PdfScanComponent.ThreadPoolSize;
        }

        /// <summary></summary>
        public void SaveSettings()
        {
            this._PdfScanComponent.FileListInputXmlFilePath = this.txt_FileListXmlFilePath.Text;
            this._PdfScanComponent.ResultFilePath = this.txt_OutputFilePath.Text;
            this._PdfScanComponent.Pattern = this.txt_Pattern.Text;
            this._PdfScanComponent.MatchScoreThreshold = (double) this.num_MinScore.Value;
            this._PdfScanComponent.ThreadPoolSize = (int) this.num_ThreadPoolSize.Value;
            this._PdfScanComponent.LogFilePath = this.txt_LogFilePath.Text;
            this._PdfScanComponent.ErrorFilePath = this.txt_ErrorFilePath.Text;
        }

        /// <summary></summary>
        public TestResult Test()
        {
            return new TestResult(true,"Success");
        }

        #endregion

        private void btn_BroseDocFolder_Click(object sender, EventArgs e)
        {
            OpenFileDialog dlg=new OpenFileDialog();
            dlg.Filter = "Xml files(*.xml)|*.xml|All files(*.*)|*.*";
            if(dlg.ShowDialog()==System.Windows.Forms.DialogResult.OK)
            {
                this.txt_FileListXmlFilePath.Text = dlg.FileName;
            }
        }

        private void btn_BrowseLog_Click(object sender, EventArgs e)
        {
            SaveFileDialog dlg=new SaveFileDialog();
            dlg.Filter = "Log files(*.log)|*.log|All files(*.*)|*.*";
            if(dlg.ShowDialog()==DialogResult.OK)
            {
                this.txt_LogFilePath.Text = dlg.FileName;
            }
        }

        private void btn_BrowseErr_Click(object sender, EventArgs e)
        {
            SaveFileDialog dlg = new SaveFileDialog();
            dlg.Filter = "Err files(*.err)|*.err|All files(*.*)|*.*";
            if (dlg.ShowDialog() == DialogResult.OK)
            {
                this.txt_ErrorFilePath.Text = dlg.FileName;
            }
        }

        private void btn_Cancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btn_OK_Click(object sender, EventArgs e)
        {
            this.SaveSettings();

            if (this.UpdateComponentSettingEvent != null)
                this.UpdateComponentSettingEvent(this._ActivityNode, this._PdfScanComponent);

            this.Close();
        }

        private void btn_SaveOutpuptTo_Click(object sender, EventArgs e)
        {
            SaveFileDialog dlg=new SaveFileDialog();
            dlg.Filter = "Xml files(*.xml)|*.xml|All files(*.*)|*.*";
            if(dlg.ShowDialog()==DialogResult.OK)
            {
                this.txt_OutputFilePath.Text = dlg.FileName;
            }
        }
    }
}
